﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms
{
    public partial class popupChangeTable : Form
    {
        private frmTableService tableService = null;
        private int BillIDFrom = -1;
        /// <summary>
        /// 1: Gộp bàn
        /// 2: Chuyển bàn
        /// </summary>        
        private int TypeChange = 1;
        private string tableFromName = "";
        private string tableToName = "";
        public popupChangeTable()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="m_billIDFrom"></param>
        /// <param name="m_type">
        /// 1: Gộp bàn
        /// 2: Chuyển bàn
        /// </param>
        public popupChangeTable(frmTableService m_tableService, int m_billIDFrom,string tFromName, int m_type)
        {
            InitializeComponent();
            tableService = m_tableService;
            BillIDFrom = m_billIDFrom;
            tableFromName = tFromName;
            TypeChange = m_type;
            if (TypeChange == 1)
                this.Text = "Chọn bàn gộp tới";
            else
                this.Text = "Chọn bàn di chuyển tới";
        }   

        private void frmTableService_Load(object sender, EventArgs e)
        {
            tabControl.Name = "tabRange";

            CtrTable ctrT = new CtrTable();
            var lastNameList = ctrT.RangeGetByStatus(1);
            foreach (var item in lastNameList)
            {

                TabPage tabPage = new TabPage();
                tabPage.Tag = item.RangeID;
                tabPage.Name = "tabPage" + item.RangeID;
                tabPage.Text = item.RangeName;

                ListView lv1 = new ListView();
                lv1.Name = "listViewTableOn" + item.RangeID;
                lv1.Dock = DockStyle.Fill;
                lv1.LargeImageList = imgListTable;
                var tables = ctrT.TableGetOn(item.RangeID);
                foreach (var table in tables)
                {
                    if (((TypeChange == 1) && (table.BillID.HasValue) && (table.BillID != BillIDFrom)) || ((TypeChange == 2) && (!table.BillID.HasValue)))
                    {
                        ListViewItem ListViewItem1 = new ListViewItem(table.TableName);
                        ListViewItem1.ImageIndex = table.BillID.HasValue ? 1 : 0;
                        ListViewItem1.Tag = table.TableID.ToString() + "+" + (table.BillID ?? -1).ToString();
                        lv1.Items.Add(ListViewItem1);
                    }
                }
                lv1.MultiSelect = false;
                lv1.View = View.LargeIcon;
                lv1.BorderStyle = BorderStyle.None;
                lv1.SelectedIndexChanged += new EventHandler(lv1_SelectedIndexChanged);
                lv1.ContextMenuStrip = contextMenuStrip1;
                tabPage.Controls.Add(lv1);


                tabControl.Controls.Add(tabPage);
            }
            this.splitContainer1.Panel1.Controls.Add(tabControl);

            tabControl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            // Why isn't there a convenient enum value for 'All'? 
            tabControl.Location = new Point(0, 10);
            tabControl.Size = new Size(tabControl.Parent.ClientSize.Width
             , tabControl.Parent.ClientSize.Height - (0 + 10)
             );           
        }
        private int billID = -1;
        private int tableID = -1;
        private void lv1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView lv1 = (ListView)sender;
            if (lv1.SelectedItems.Count <= 0)
                return;
            foreach (ListViewItem table in lv1.Items)
            {
                if (table.BackColor != Color.Transparent)
                    table.BackColor = Color.Transparent;
            }
            lv1.SelectedItems[0].BackColor = Color.FromArgb(220, 220, 220);
            tableID = Convert.ToInt32(lv1.SelectedItems[0].Tag.ToString().Split('+')[0]);
            billID = Convert.ToInt32(lv1.SelectedItems[0].Tag.ToString().Split('+')[1]);
            tableToName = "\"" + lv1.SelectedItems[0].Text + "\" (" + tabControl.SelectedTab.Text + ")";
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshCurrentTab();
        }  

        public void RefreshCurrentTab()
        {
            int rangeID = Convert.ToInt32(tabControl.SelectedTab.Tag);
            var contls = tabControl.Controls.Find("listViewTableOn" + rangeID, true);
            if (contls.Length > 0)
            {
                try
                {
                    var lv1 = (ListView)contls[0];
                    lv1.Items.Clear();
                    CtrTable ctrT = new CtrTable();
                    var tables = ctrT.TableGetOn(rangeID);
                    foreach (var table in tables)
                    {
                        if (((TypeChange == 1) && (table.BillID.HasValue)&&(table.BillID!=BillIDFrom)) || ((TypeChange == 2) && (!table.BillID.HasValue)))
                        {
                            ListViewItem ListViewItem1 = new ListViewItem(table.TableName);
                            ListViewItem1.ImageIndex = table.BillID.HasValue ? 1 : 0;
                            if (tableID == table.TableID)
                                ListViewItem1.Selected = true;
                            ListViewItem1.Tag = table.TableID.ToString() + "+" + (table.BillID ?? -1).ToString();
                            lv1.Items.Add(ListViewItem1);
                        }
                    }
                }
                catch { }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {            
            CtrProduct ctrP = new CtrProduct();
            int ret=0;
            string des = "";
            if (TypeChange == 1)
            {
                if (billID <= 0)
                {
                    MessageBox.Show("Bạn chưa chọn bàn!", "Thông báo");
                    return;
                }
                ret=ctrP.BillGross(BillIDFrom, billID);
                des = "Gộp bàn " + tableFromName + " tới " + tableToName;
            }
            else
            {
                if (tableID <= 0)
                {
                    MessageBox.Show("Bạn chưa chọn bàn!", "Thông báo");
                    return;
                }
                ret=ctrP.BillMove(BillIDFrom, tableID);
                des = "Chuyển bàn " + tableFromName + " tới " + tableToName;               
            }
            if (ret <= 0)
            {
                switch (ret)
                {
                    case -2:
                    case -4:
                        MessageBox.Show("Bàn hiện tại chưa mở", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    case -3:
                        MessageBox.Show("Bàn gộp đến chưa mở", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    case -5:
                        MessageBox.Show("Bàn chuyển đến đang được mở", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    case -6:
                        MessageBox.Show("Bàn chuyển đến không tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    default:
                        if (TypeChange == 1)
                            MessageBox.Show("Lỗi gộp bàn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        else
                            MessageBox.Show("Lỗi chuyển bàn bàn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                }
            }
            else
            {
                CtrAdmin ctra = new CtrAdmin();
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 2, des, "Quản lý dịch vụ");
            }
            tableService.RefreshCurrentTab();
            tableService.RefreshAndSelectTabOther(Convert.ToInt32(tabControl.SelectedTab.Tag), tableID);
            this.Close();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            try
            {
                var contextMenuStrip1 = (ContextMenuStrip)sender;
                var control1 = (ListView)contextMenuStrip1.SourceControl;
                if (control1.SelectedItems.Count != 1)
                {
                    contextMenuStrip1.Items["chooseToolStripMenuItem"].Enabled = false;                 
                }
                else
                {
                    contextMenuStrip1.Items["chooseToolStripMenuItem"].Enabled = true;                     
                }
            }
            catch
            {
                contextMenuStrip1.Items["chooseToolStripMenuItem"].Enabled = false;
            }
        }      
       
    }
}
